package com.shqcjd.siemensglass;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Bundle;
import android.widget.Toast;

import com.orhanobut.logger.Logger;
import com.shqcjd.siemensglass.base.BaseActivity;
import com.shqcjd.siemensglass.bean.AgoraIdBean;
import com.shqcjd.siemensglass.bean.LoginBean;
import com.shqcjd.siemensglass.http.ApiManager;
import com.shqcjd.siemensglass.http.ApiService;
import com.shqcjd.siemensglass.http.BaseResult;
import com.shqcjd.siemensglass.http.Response;
import com.shqcjd.siemensglass.http.ResponseData;
import com.shqcjd.siemensglass.http.RxHttp;
import com.shqcjd.siemensglass.ui.SelectListActivity;
import com.shqcjd.siemensglass.utils.GlobalParams;
import com.shqcjd.siemensglass.utils.PrefUtils;
import com.shqcjd.siemensglass.utils.UIUtils;

import java.util.List;

import es.dmoral.toasty.Toasty;
import io.agora.AgoraAPI;
import io.agora.IAgoraAPI;

import static com.shqcjd.siemensglass.utils.Utils.getAndroidId;

public class SplashActivity extends BaseActivity {


    private String androidId;
    private String my_token;
    private Context mContext;

    @Override
    public int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        mContext = this;
        androidId = getAndroidId(this);
        login();
    }

    private void login() {
        new RxHttp<BaseResult<LoginBean>>().send(ApiManager.getService().getLogin(androidId),new Response<BaseResult<LoginBean>>(this){
            @Override
            public void onNext(BaseResult<LoginBean> result) {
                super.onNext(result);

                if (result.code == 0) {//正确
                    Logger.e("login:id:" + result.data.user.id);
                    Logger.e("login:token结果:" + result.data.token);
//                    new TagAliasUtils().TagAliasUtils(GlobalParams.getUserId());//推送设备别名
                    my_token = result.data.token;
                    GlobalParams.saveUserId(result.data.user.id);
                    GlobalParams.saveuser_token(my_token);
                    PrefUtils.setString(mContext, PrefUtils.USER_NAME,result.data.user.name);
                    int status = MyApp.the().getmAgoraAPI().getStatus();
                    if (status == 2) {//2: 用户已登录。
                        goIndex();
                    } else {
                        MyApp.the().getmAgoraAPI().login2(UIUtils.getSaveAgoraAppId(mContext), androidId, "_no_need_token", 0, "", 5, 1);
                    }

                } else {
                    Toasty.info(SplashActivity.this,"网络连接异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void goIndex() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //延迟两秒钟执行
                startActivity(new Intent(mActivity, SelectListActivity.class));
                finish();
            }
        }, 1000);
    }

    @Override
    public void loadData() {
        getAgoraAppid();
    }

    private void addCallback() {
        MyApp.the().getmAgoraAPI().callbackSet(new AgoraAPI.CallBack() {

            @Override
            public void onLoginSuccess(int i, int i1) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Logger.e("信令登录成功");
//                        GlobalParams.saveuser_token(my_token);
                        goIndex();

                    }
                });
            }

            @Override
            public void onLogout(int i) {
                Logger.e("信令登出");
            }

            @Override
            public void onLoginFailed(final int i) {
                Logger.e("信令onLoginFailed");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (i == IAgoraAPI.ECODE_LOGIN_E_NET) {
                            Toasty.warning(SplashActivity.this,"用户登录失败,请退出重新登录!", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }

            @Override
            public void onError(String s, int i, String s1) {
                Logger.e("信令onError s:" + s + " i:" + i + " s1:" + s1);
                if (i == 208) {//用户已登录，再次发起登录会触发该错误 可以忽视该错误

                }
            }

        });
    }



    private void getAgoraAppid() {
        new RxHttp<ResponseData<List<AgoraIdBean>>>().send(ApiManager.getService().getAgoraId(),new Response<ResponseData<List<AgoraIdBean>>>(this){
            @Override
            public void onNext(ResponseData<List<AgoraIdBean>> retData) {
                super.onNext(retData);
                if (retData.retCode == 10000) {
                    for (AgoraIdBean bean : retData.data) {
                        if (bean.projectName.equals(getResources().getString(R.string.app_name))) {
                            String json = bean.projectIds.toString();
                            PrefUtils.setString(mContext,PrefUtils.APPID_LIST,json);
                        }
                    }

                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        addCallback();
    }
}
